#include "stdafx.h"
#include "CombatPowerAddon.h"
#include "InstanceTouch.h"
#include "BuffDef.h"
#include <algorithm>

DECL_INSTANCE_TOUCH(ETouch_CombatPowerAddon);

struct CombatPowerAddonReflectionRegister
{
	CombatPowerAddonReflectionRegister()
	{
		REGISTER_REFLECTION_MEMBER(CombatPowerAddon,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(CombatPowerAddon,m_sDisplayName,std::string("DefaultDisplayName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(CombatPowerAddon,m_iPowerValue,0).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(CombatPowerAddon,m_vAddonBuffIDs,std::vector<int32>()).Editor_SetAsForeignKeySet(eDefGroup_Default,eDefaultDef_Buff);	
		REGISTER_REFLECTION_MEMBER(CombatPowerAddon,m_sDesc,std::string("sDefaultDesc")).Editor_SetAsRaw();

		GlobalClassManager::Instance().RegisterClass(CombatPowerAddon::s_GetReflection(),eDefaultDef_CombatPowerAddon);
		GlobalDefManager::Instance().RegisterDefManger(&CombatPowerAddonManager::Instance());
	}
}___combatpoweraddonreflectionregister;

rewrite  void CombatPowerAddon::OnArchieveReaded()
{
	m_vBuffs.clear();
	for(uint32 ii=0;ii<m_vAddonBuffIDs.size();ii++)
	{
		BuffDef * pBuffDef=BuffDefManager::Instance().LookupItem(m_vAddonBuffIDs[ii]);
		if(NULL!=pBuffDef) m_vBuffs.push_back(pBuffDef);
		
	}
}
static bool ComparePowerAddon(const CombatPowerAddon * pA,const CombatPowerAddon *pB)
{
	return pA<pB;
}
rewrite  void CombatPowerAddonManager::OnArchieveReaded()
{
	___CombatPowerAddonManager::OnArchieveReaded();
	m_vSortedPowerAddons.clear();
	Enumerate(m_vSortedPowerAddons);
	std::sort(m_vSortedPowerAddons.begin(),m_vSortedPowerAddons.end(),ComparePowerAddon);

}


const CombatPowerAddon * CombatPowerAddonManager::GetCurrentAddon( int32 iCurrentPower ) const
{
	const CombatPowerAddon * pRet=NULL;
	for(uint32 ii=0;ii<m_vSortedPowerAddons.size();ii++)
	{
		const CombatPowerAddon * pAddon=m_vSortedPowerAddons[ii];
		if(pAddon->m_iPowerValue<=iCurrentPower) pRet=pAddon;
	}
	return pRet;
}